/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: property_service.h
 *
 * Purpose: property service implementation
 *
 * Developer:
 *   wen.gu , 2019-10-05
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __PANDA_PROPERTY_SERVICE_H__
#define __PANDA_PROPERTY_SERVICE_H__

#include <mutex>

#include "panda/core/types.h"
#include "panda/core/errors.h"
#include "panda/sys/ipc_service_provider.h"
#include "panda/per/key_value_storage.h"

namespace panda
{

/******************************************************************************
 **    MACROS
 ******************************************************************************/

 /******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
/** error code of panda */

 /******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
class CORE_CLASS PropertyService
{
public:
    using PandaErrc = panda::core::PandaErrc;
    using KeyValueStorage = panda::per::KeyValueStorage;
    using IpcServiceProvider = panda::sys::IpcServiceProvider;
    using IpcServiceRequestHandlePtr = panda::sys::IpcServiceRequestHandlePtr;
    using IpcPacketData = panda::sys::IpcPacketData;

    PropertyService();
    ~PropertyService();

public:
    PandaErrc initialized(const std::string& defPropFile);

    PandaErrc start();
    PandaErrc stop();
public:
    /** get property from service
     * reqHdlr [in] response handle
     * method  [in] method name
     * param   [in] the parameter for request
     *              [mandatory ] name:  the name of property key
     *
     * reply   [out]: [mandatory ] code : error code
     *                [mandatory ] descroption: the descriptor for error code
     *                [optional]   prop: the value of property.
     *
     */
    PandaErrc get(IpcServiceRequestHandlePtr reqHdl, const std::string& method, const IpcPacketData& param);

    /** set property to service
     * reqHdlr [in] response handle
     * method  [in] method name
     * param   [in] the parameter for request
     *              [mandatory ] name:  the name of property key
     *              [mandatory ] prop:  the value of property
     *
     * reply   [out]: [mandatory ] code : error code
     *                [mandatory ] descroption: the descriptor for error code
     *
     */
    PandaErrc set(IpcServiceRequestHandlePtr reqHdl, const std::string& method, const IpcPacketData& param);

protected:
    bool parsePropertyFile(const std::string& propFile);
    bool parsePersistProp();
    /** valid: true, invalid: false */
    bool checkPropKey(const std::string& key);
    bool startWith(const std::string& str, const std::string& prefix);
    PandaErrc writePersistentProperty(const std::string& key, const std::string& val);
private:
    std::shared_ptr <IpcServiceProvider> mSrv = nullptr;
    std::shared_ptr<KeyValueStorage> mKVS = nullptr;
    std::mutex mLock;
};

} /** namespace panda*/

#endif /** !__PANDA_PROPERTY_SERVICE_H__ */
